package com.biz.primus.ms.promotion.service.promotion;

import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.base.vo.PageVo;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.model.promotion.exception.PromotionExceptionType;
import com.biz.primus.model.promotion.vo.LuckyOrderPromotionVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.promotion.dao.po.LuckyOrderPromotion;
import com.biz.primus.ms.promotion.dao.repository.LuckyOrderPromotionRepository;
import com.google.common.collect.Lists;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 下单有礼后台管理Service
 *
 * @author david-liu
 */
@Service
public class LuckyOrderPromotionManagementServiceImpl extends AbstractBaseService implements LuckyOrderPromotionManagementService {

    @Autowired
    private LuckyOrderPromotionRepository luckyOrderPromotionRepository;

    @Override
    public Boolean updateLuckyOrderPromotion(LuckyOrderPromotionVo vo) {
        AssertUtils.notNull(vo, PromotionExceptionType.ILLEGAL_PARAMETER);
        LuckyOrderPromotion luckyOrderPromotion;
        if (Objects.isNull(vo.getId())) {
            luckyOrderPromotion = new LuckyOrderPromotion();
            luckyOrderPromotion.setId(idService.getNextId());
        } else {
            luckyOrderPromotion = luckyOrderPromotionRepository.findOne(vo.getId());
            AssertUtils.notNull(luckyOrderPromotion, PromotionExceptionType.ILLEGAL_STATE);
        }
        luckyOrderPromotion.setName(vo.getName());
        luckyOrderPromotion.setDescription(vo.getDescription());
        luckyOrderPromotion.setChannels(vo.getChannels());
        luckyOrderPromotion.setStartTime(LocalDateTime.from(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").parse(vo.getStartTime())));
        luckyOrderPromotion.setEndTime(LocalDateTime.from(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").parse(vo.getEndTime())));
        luckyOrderPromotion.setEffectMemberLevels(Optional.ofNullable(vo.getEffectMemberLevels()).orElse(Lists.newArrayList()).stream().map(Integer::valueOf).collect(Collectors.toList()));
        luckyOrderPromotion.setAllowCoupon(vo.getAllowCoupon());
        luckyOrderPromotion.setScope(vo.getScope());
        luckyOrderPromotion.setScopeIds(Arrays.stream(StringUtils.split(vo.getScopeIds(), ",")).map(id -> Long.valueOf(StringUtils.trim(id))).collect(Collectors.toList()));
        luckyOrderPromotion.setCommonStatus(vo.getCommonStatus());
        return null;
    }

    @Override
    public SpringPageVO<LuckyOrderPromotionVo> getLuckyOrderPromotions(PageVo pageVo) {
        return null;
    }

    @Override
    public LuckyOrderPromotionVo getLuckyOrderPromotion(Long promotionId) {
        return null;
    }

    @Override
    public Boolean toggleLuckyOrderPromotionStatus(Long promotionId) {
        return null;
    }
}
